export default {
    name: "Category",
    data() {
        return {
            curCatID: null,
            activeKey: 0,
            catList: [],
            goodList: [],
            loading: false,
            error: false,
            finished: false,
            refreshing: false,
            queryInto: {
                pagenum: 1,
                pagesize: 6,
            },
        }
    },
    created() {
        this.getCatList()
    },
    methods: {
        getCatList() {
            let api = this.$http.api.category.getTree()
            this.$http.callApi(api).then((res) => {
                this.catList = res.data || [];
                this.curCatID = this.catList[this.activeKey].id;
                this.onload();
            });
        },
        onload() {
            let pagenum = this.queryInto.pagenum;
            let pagesize = this.queryInto.pagesize;
            let api = this.$http.api.good.getPage(pagenum, pagesize);
            this.$http
                .callApi(api, {
                    data: {
                        cat_one_id: this.curCatID
                    },
                })
                .then((res) => {
                    if (this.refreshing) {
                        this.goodList = [];
                        this.refreshing = false;
                    }
                    this.goodList.push(...res.data.good);
                    if (this.goodList.length >= res.data.total) {
                        this.finished = true;
                    } else {
                        this.queryInto.pagenum++;
                    }
                    this.loading = false;
                })
                .catch(err => {
                    this.loading = false;
                    this.error = true;
                })
        },
        onRefresh() {
            this.queryInto.pagenum = 1;
            this.onload();
        },
        onChange() {
            this.curCatID = this.catList[this.activeKey].id;
            this.goodList = [];
            this.queryInto.pagenum = 1;
            this.onload();
        }
    }
};